﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SolutionBLL;
using SolutionModel;

namespace MvcUI.Areas.AdminWeb.Controllers.AdminSystem
{
    public class MenuController : Controller
    {
        //
        // GET: /AdminWeb/Menu/
        T_MenuBLL bll = new T_MenuBLL();
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Edit()
        {
            ViewData["action"] = "/adminweb/menu/editsave";
            return View("edit");
        }

        public ActionResult Add()
        {
            ViewData["action"] = "/adminweb/menu/addsave";
            return View("edit");
        }

        public JsonResult addSave(T_Menu menu)
        {
            MessageInfoByFormModel info = new MessageInfoByFormModel();
            try
            {
                int rowCount = bll.InsertMenus(menu);
                if (rowCount > 0)
                {
                    info.info = "菜单添加成功！";
                    info.status = "y";
                }
                else
                {
                    info.info = "菜单添加失败！";
                    info.status = "n";
                }
                return Json(info);
            }
            catch (Exception emsg)
            {
                info.info = emsg.Message;
                info.status = "n";
                return Json(info);
            }
        }
        public JsonResult editSave(T_Menu menu)
        {
            MessageInfoByFormModel info = new MessageInfoByFormModel();
            try
            {
                int re = bll.VerifyParent(Convert.ToInt32(menu.C_Parent_ID), menu.C_ID);
                if (re > 0)
                {
                    info.info = "不可把“菜单本身”或“子菜单”当做本菜单的“父菜单”，请重新选择父菜单项！";
                    info.status = "y";
                    return Json(info);
                }
                int rowCount = bll.UpdateMenu(menu);
                if (rowCount > 0)
                {
                    info.info = "菜单保存成功！";
                    info.status = "y";
                }
                else
                {
                    info.info = "菜单保存失败！";
                    info.status = "n";
                }
                return Json(info);
            }
            catch (Exception emsg)
            {
                info.info = emsg.Message;
                info.status = "n";
                return Json(info);
            }
        }



        public JsonResult getTree()
        {
            T_MenuBLL bll = new T_MenuBLL();
            List<TreeGridModel> list = bll.GetTree();
            return Json(list);
        }

        public JsonResult updateSort(int cid, int pid, int targetid = 0)
        {
            T_MenuBLL bll = new T_MenuBLL();
            if (cid != 0)
            {
                bll.SortMenu(cid, targetid, pid);
            }
            return null;
        }

        public JsonResult GetTreeNotInCurrent(String cid)
        {
            List<TreeGridModel> list = bll.GetTreeNotInCurrent(cid);
            list = new List<TreeGridModel>(){
                new TreeGridModel(){
                    ID = 0,
                    PID = 0,
                    Name = "根目录",
                    children =list
                }
            };
            return Json(list);
        }

        public JsonResult delMenu(int id)
        {
            MessageInfoByFormModel info = new MessageInfoByFormModel();

            if (bll.GetTreeByPid(id).Count() > 0)
            {
                info.info = "当前分类有子分类，不可以删除，请先删除子分类再删除此分类。";
                info.status = "n";
                return Json(info);
            }

            bll.delMenuID(id);
            info.info = "删除成功！";
            info.status = "y";
            return Json(info);
        }
    }
}
